<template>
  <el-dialog
    :title="`${ser}权限点`"
    width="40%"
    :visible="dialogVisible"
    @close="btnCancel"
  >
    <el-form ref="formData" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="formData.description" style="width:90%" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch
          v-model="formData.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <!-- 确定 取消按钮 -->
    <el-row type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="addPermission(1)">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { addPermissionsAPI, getPermissionsAPIxq, editPermissionsAPI } from '@/api/permisson'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    type: {
      type: Number,
      default: null
    },
    id: {
      type: Number,
      default: null
    },
    ser: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认关闭
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    addPermission() {
      this.$refs.formData.validate(async(isOk) => {
        if (isOk) {
          if (this.ser === '编辑') {
            // console.log(123456, this.formData)
            await editPermissionsAPI(this.formData)
            this.$message.success('编辑成功')
          } else {
            this.formData.type = this.type
            this.formData.pid = this.id
            await addPermissionsAPI(this.formData)
            this.$message.success('添加成功')
          }
          this.$emit('sxym')
        }
        this.btnCancel()
      })
    },
    btnCancel() {
      this.$refs.formData.resetFields()
      this.formData.description = ''
      this.formData.enVisible = ''
      this.$emit('update:dialogVisible', false)
    },
    async getPermissionsXq() {
      this.formData = await getPermissionsAPIxq(this.id)
      // console.log(this.formData)
    }
  }
}
</script>

<style scoped>

</style>
